<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/26
 * Time: 22:04
 */
namespace Admin\Controller;
use Think\Controller;
class PublicController extends Controller{

     public function login(){
         $this->display();
     }

     //验证码
    public function captcha(){
         //配置
        $cfg=array(
            'fontSize'  =>  12,              // 验证码字体大小(px)
            'useCurve'  =>  true,            // 是否画混淆曲线
            'useNoise'  =>  false,            // 是否添加杂点
            'imageH'    =>  35,               // 验证码图片高度
            'imageW'    =>  80,               // 验证码图片宽度
            'length'    =>  4,               // 验证码位数
            'fontttf'   =>  '4.ttf',              // 验证码字体，不设置随机获取
        );
        $verifg=new \Think\Verify($cfg);
        $verifg->entry();
    }

    //登录验证
    public function checkLogin(){
        $post=I('post.');
        $verify=new \Think\Verify();
        $result=$verify->check($post['captcha']);
        if($result){
            $model=M('user');
            unset($post['captcha']);
            $val= $post['username'];
            $where="username = '$val'";
            $data=$model->where($where)->select();
            if($data){
                session("id",$data[0]['id']);
                session('username',$data[0]['username']);
                session('password',$data[0]['password']);
                session('role_id',$data[0]['role_id']);
                $this->success('登录成功',U("Index/index"),3);
            }else{
                $this->error('用户名或密码错误');
            }
        }else{
            $this->error('验证码错误');
        }
    }

    //退出
    public function loginOut(){
        session('id',null);
        session('username',null);
        session('password',null);
        $this->success("退出成功",U('login'),3);
    }
}